const { get, post, put } = require('axios')
const os = require('os')
const { exec } = require('child_process')
const fs = require('fs')

const MB = 1024 * 1024

const main = config => {
  return {
    /**内存信息
     *
     * @returns {{total: number, free: number}}
     */
    memory() {
      return {
        total: os.totalmem() / MB,
        free: os.freemem() / MB,
        swap: 0,
      }
    },
    uptime() {
      return os.uptime() / 60
    },
    cpu() {
      const info = os.cpus()
      const data = info[0]
      return {
        cores: info.length,
        speed: data.speed,
        times: data.times,
        model: data.model,
      }
    },
    hostname() {
      return os.hostname()
    },
    loadavg() {
      const info = os.loadavg()
      return {
        0: info[0],
        5: info[1],
        15: info[2],
      }
    },
    disk() {
      return {
        total: 20450,
        free: 11210,
        avg: this.limit30(),
      }
    },
    limit30() {
      const data = Math.random() * 100
      if (data > 30) {
        return this.limit30()
      }
      return data.toString().slice(0, 4)
    },
    net() {
      let content = fs.readFileSync('/proc/net/dev').toString()
      // 第二行开始
      content = content.split('\n').slice(2)
      return content.map(row => {
        row = row.replace(/^\s+/, '').split(/\s+/)
        return {
          name: row[0].replace(':', ''),
          receive: {
            bytes: row[1],
            packets: row[2],
            errs: row[3],
            drop: row[4],
            fifo: row[5],
            frame: row[6],
            compressed: row[7],
            multicast: row[8],
          },
          send: {
            bytes: row[9],
            packets: row[10],
            errs: row[11],
            drop: row[12],
            fifo: row[13],
            colls: row[14],
            carrier: row[15],
            compressed: row[16],
          },
        }
      }).filter($ => $.name)
    },
    async process() {
      return new Promise(resolve => {
        const list = []
        exec('ps aux', async (err, stdout, stderr) => {
          if (err) {
            return console.error(err)
          }
          const data = stdout.split('\n')
          data.filter((line, index) => {
            let processMessage = line.trim().split(/\s+/)
            let processName = processMessage[0]
            list.push({
              name: processMessage[0],
              pid: processMessage[1],
              cpu: processMessage[2],
              memory: processMessage[3],
            })
            if (index === data.length - 1) {
              resolve(list.slice(1))
            }
          })
        })
      })
    },
    nginx() {
      const reg = /([^ ]*) ([^ ]*) ([^ ]*) (\[.*]) (.*?) (-|[0-9]*) (-|[0-9]*) (.*?) (.*?)/
      let content = fs.readFileSync('access.log').toString().split('\n')
      const index = Math.floor(Math.random() * 300)
      let data = content[index]
      if (data) {
        data = data.match(reg)
        if (!data) {
          return this.nginx()
        }
        const info = {
          ip: data[1],
          date: Date.now(),
          status: Number(data[6]),
          path: data[5].replace(/"/g, ''),
          host: data[8].replace(/"/g, ''),
          agent: data[9],
        }
        if (!info.status) {
          return this.nginx()
        }
        return info
      } else {
        return this.nginx()
      }
    },
  }
}
module.exports = main

